<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/logo.png" alt="" width="55px">
                <h3>小邹电商后台管理系统</h3>
            </div>
            <div>
                <el-badge value="new" class="item">
                    <el-tooltip effect="dark" content="查看邮件" placement="top">
                        <el-button type="primary" icon="el-icon-message" circle></el-button>
                    </el-tooltip>
                </el-badge>
                <el-badge value="hot" class="item">
                    <el-tooltip effect="dark" content="查看收藏" placement="top">
                        <el-button type="warning" icon="el-icon-star-off" circle></el-button>
                    </el-tooltip>
                </el-badge>
                <el-button type="info" @click="logout">退出</el-button>
            </div>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 判断是否折叠来改变宽度-->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">
                    <!-- 判断是否折叠来改变图标样式 -->
                    <el-button type="info" :icon="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
                        class="toggle-btn"></el-button>
                </div>
                <!-- 侧边栏菜单区域 unique-opened每次只能展开唯一的菜单项 collapse是否水平折叠菜单 default-active当前激活菜单的index设置高亮效果-->
                <!-- collapse-transition设置折叠动画关闭更流畅 -->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened
                    :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
                    <!-- 一级菜单 index值只能是字符串不能是数值所以id(数值)+''转为字符串-->
                    <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                        <!-- 一级菜单的模板区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{ item.authName }}</span>
                        </template>

                        <!-- 二级菜单 path要以/开头-->
                        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                            @click="saveNavState('/' + subItem.path)">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <span>{{ subItem.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容主体 -->
            <el-main>
                <!-- welcome路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
  
<script>
export default {
    data() {
        return {
            // 左侧菜单数据
            menulist: [],
            //遍历一级菜单图标利用id
            iconsObj: {
                '125': 'iconfont icon-users',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao'
            },
            // 是否折叠
            isCollapse: false,
            // 保存被激活的链接地址
            activePath: ''
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
        //清空token
        //后续请求不会携带token,必须重新登录生成一个新的token之后才能访问页面
        logout() {
            window.sessionStorage.clear()
            //跳转到登录页
            this.$router.push('/login')
        },
        // 获取所有的菜单 async和await一起使用 简化promise操作
        async getMenuList() {
            const { data: res } = await this.$http.get('menus')
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menulist = res.data
            //console.log(res)
        },
        // 点击按钮，切换菜单的折叠与展开
        toggleCollapse() {
            this.isCollapse = !this.isCollapse
        },
        // 保存链接的激活状态
        saveNavState(activePath) {
            window.sessionStorage.setItem('activePath', activePath)
            this.activePath = activePath
        }
    }
}
</script>
  
<style lang="less" scoped>
.home-container {
    height: 100%;
}

.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between; //左右贴边对齐
    padding-left: 0;
    align-items: center; //纵向居中对齐
    color: #fff;
    font-size: 20px;

    >div {
        display: flex;
        align-items: center;

        h3 {
            margin-left: 15px;
        }
    }

    .item {
        margin-right: 30px;
    }
}

.el-aside {
    background-color: #333744;

    .el-menu {
        border-right: none; //去除对不齐的边框线
    }
}

.el-main {
    background-color: #e2e8ef;
}

.iconfont {
    margin-right: 10px;
}

.toggle-button {
    background-color: #45474c;
    font-size: 10px;
    line-height: 10px;
    color: #fff;
    text-align: center;
    cursor: pointer;

    .toggle-btn {
        background-color: #45474c;
        border: 0;
        font-size: 17px;
        line-height: 10px;
    }
}
</style>
  